﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace AgriMarketService
{
    public class Statistician
    {
        public static List<YProduct> GetYearStatistic(int year)
        {
            AgriMarketDataClassesDataContext db = new AgriMarketDataClassesDataContext();
            List<Tbl_Auction> finishedAuctions = (from auction in db.Tbl_Auctions
                                           where auction.FinishedDate < DateTime.Now && auction.FinishedDate.Year==year
                                           select auction
                                               ).ToList();
            List<Tbl_Auctioneer> winners=new List<Tbl_Auctioneer>();
            foreach (var fAuction in finishedAuctions)
	        {
                Tbl_Auctioneer winner = (from auctioneer in db.Tbl_Auctioneers
                                         where auctioneer.AuctionId == fAuction.AuctionId
                                         orderby auctioneer.Price descending
                                         select auctioneer).FirstOrDefault();
                if(winner!=null)
                    winners.Add(winner);
	        }
            List<YProduct> result = (from auction in finishedAuctions
                          join win in winners on auction.AuctionId equals win.AuctionId
                          group win by auction.ProductId into average
                          select new YProduct() { 
                              StartYear=year,
                              FinishedYear=year,
                              Product=Product.GetProductById(average.Key),
                              Price=average.Average(win=>win.Price)
                          }).ToList();
            return result;
        }
        public static List<YProduct> GetYearStatisticInDuration(int start,int finish)
        {
            AgriMarketDataClassesDataContext db = new AgriMarketDataClassesDataContext();
            List<Tbl_Auction> finishedAuctions = (from auction in db.Tbl_Auctions
                                                  where auction.FinishedDate < DateTime.Now && auction.FinishedDate.Year >=start && auction.FinishedDate.Year<=finish
                                                  select auction
                                               ).ToList();
            List<Tbl_Auctioneer> winners = new List<Tbl_Auctioneer>();
            foreach (var fAuction in finishedAuctions)
            {
                Tbl_Auctioneer winner = (from auctioneer in db.Tbl_Auctioneers
                                         where auctioneer.AuctionId == fAuction.AuctionId
                                         orderby auctioneer.Price descending
                                         select auctioneer).FirstOrDefault();
                if (winner != null)
                    winners.Add(winner);
            }
            List<YProduct> result = (from auction in finishedAuctions
                                     join win in winners on auction.AuctionId equals win.AuctionId
                                     group win by auction.ProductId into average
                                     select new YProduct()
                                     {
                                         StartYear = start,
                                         FinishedYear = finish,
                                         Product = Product.GetProductById(average.Key),
                                         Price = average.Average(win => win.Price)
                                     }).ToList();
            return result;
        }
    }
}